package com.cf.mapper;

import com.cf.pojo.User;
import com.cf.pojo.UserDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author Chengfei
 * @Description
 * @date 2023/6/24
 */

/**
 * componentModel 制定模式spring  将bean交给spring管理
 * unmappedTargetPolicy ignore 目标属性未匹配时系统忽略，不打印警告日志
 *  uses 公共的转换组件引用
 */
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE ,uses = {ConvertUtil.class})
public interface UserMapper {


    //静态成员
    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    // 常量
    @Mapping(source = "id", target = "userId",constant = "可以指定常量")
    // 默认值
    @Mapping(source = "name", target = "userName", defaultValue = "可以指定默认值")
    // 日期转换
    @Mapping(source = "crateTime", target = "beginTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    UserDto userToUserDto(User user);

    // 批量写法
    @Mappings({
            // Integer 到 String的转换
            @Mapping(source = "id", target = "numId" ,numberFormat = "$#.00"),
            @Mapping(source = "name", target = "userName"),
            //Date 转字符串
            // @Mapping(source = "crateTime", target = "crtTime", dateFormat = "dd.MM.yyyy"),
            @Mapping(target = "crateTime", expression = "java(java.time.LocalDateTime.now()")
    })
    // UserDto userToUserDto2(User user);

    // 批量转换,循环调用上面单个转换
    List<UserDto> userToUserDtoList(List<User> users);

}
